package com.example.cloudsampleprovider.contoller;

import com.example.common.jwt.JwtUtils;
import com.example.common.jwt.UserMsg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
public class LoginController {

    @RequestMapping("/login")
    public Object login(HttpServletResponse response, @RequestBody UserMsg userMsg) {
        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(userMsg.getUserName()) &&
                StringUtils.isNotBlank(userMsg.getPassWord())) {
            userMsg.setId("123");
            userMsg.setUserRole("role");
            String jwt = JwtUtils.createJwt(userMsg);
            map.put("token", jwt);
            map.put("status", "200");
            return map;
        } else {
            map.put("status", "500");
            return map;
        }
    }
}
